package com.example.java2mysql.controller;

import com.example.java2mysql.domain.Person;
import com.example.java2mysql.infrastructure.JpaPerson;
import com.example.java2mysql.infrastructure.Randoms;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class Restful {

    @Resource
    private Randoms randoms;

    @Resource
    private JpaPerson jpaPerson;
    @PutMapping("/person")
    public Person save(@RequestBody Person person){
        person.setPersonCode(randoms.randomString("SP"));
        return jpaPerson.save(person);
    }
    @GetMapping("/persons")
    public List<Person> getAll(){
        return jpaPerson.findAll();
    }
}
